<?php

/**
 * This is the model class for table "{{training}}".
 *
 * The followings are the available columns in table '{{training}}':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $short_description
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $status
 * @property integer $image_id
 * @property Files $image
 *
 * The followings are the available model relations:
 * @property Class[] $classes
 */
class Training extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Training the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{training}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('create_time, update_time, status,image_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('short_description,description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, description, create_time, update_time, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'classes' => array(self::HAS_MANY, 'GuitarClass', 'training_id','order'=>'id DESC'),
            'classCount'=>array(self::STAT, 'GuitarClass', 'training_id'),
			'comments' => array(self::HAS_MANY, 'Comments', 'item_id','condition'=>"status=1 AND item_type='training'"),
            'image'=>array(self::BELONGS_TO, 'Files', 'image_id'),
            'commentCount' => array(self::STAT, 'Comments', 'item_id', 'condition'=>"status=1 AND item_type='training'"),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên khóa học',
			'short_description' => 'Miêu tả ngắn',
			'description' => 'Miêu tả',
			'create_time' => 'Thời gian tạo',
			'update_time' => 'Thời gian cập nhật',
			'status' => 'Trạng thái',
			'image_id' => 'Ảnh',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {
        if ($this->isNewRecord) {
            $this->create_time = time();
            $this->update_time = time();
        } else {
            $this->update_time = time();
        }
        return parent::beforeSave();
    }

    /**
     * @param $comment Comments
     * @return mixed
     */
    public function addComment($comment)
    {
        if(Yii::app()->params['commentNeedApproval'])
            $comment->status=2;
        else
            $comment->status=1;
        $comment->item_type = 'training';
        $comment->item_id=(int)$this->id;
        return $comment->save();
    }

    public function getOthers()
    {
        $criteria = new CDbCriteria(array('condition'=>"id <> $this->id",'order'=>'id DESC','limit'=>10));
        return new CActiveDataProvider($this,array('criteria'=>$criteria));
    }
    
    public function getUrl()
    {
        return Yii::app()->createUrl('training/view', array(
            'id'=>$this->id,
            'title'=>$this->name,
        ));
    }

    public function checkRoleReg($userRole,$classRole){
        if(!Yii::app()->user->isGuest){
            /**
             * @var $auth RDbAuthManager
             */
            $auth = Yii::app()->authManager;
            
            $user =  Yii::app()->user->checkAccess('premium_basic');
        }
        
        return true;
    }
}